package com.swotxu.common.conf;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 统一线程池配置
 *
 * @Date: 2021/2/14 23:19
 * @Author: swotXu
 */
@Configuration
@EnableConfigurationProperties(ThreadPoolConfigProperties.class) // 注入指定配置 Bean
public class MyThreadConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties poolProperties) {
        return new ThreadPoolExecutor(poolProperties.getCoreSize(), poolProperties.getMaxSize()
                , poolProperties.getKeepAliveTime(), TimeUnit.SECONDS
                , new LinkedBlockingQueue<>(poolProperties.getWorkQueueSize())
                , Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
